package com.lbs.view.adapter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;

import com.lbs.droiddoctor.R;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SymptomsAdapter extends BaseAdapter {

	private Context ctx;
	private ArrayList<String> symptoms;
	private Handler uiHandler;
	private boolean changeIcon;
	public SymptomsAdapter(Context ctx, ArrayList<String> symptoms,Handler uiHandler) {
		this.ctx = ctx;
		this.symptoms = symptoms;
		this.uiHandler = uiHandler;
	}
	
	public void setChangeIcon(boolean flag){
		this.changeIcon = flag;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return symptoms.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		System.out.println("SymptomsAdapter.getView()");
		View v = null;
		if (convertView != null) {
			v = convertView;
		} else {
			LayoutInflater inflater = LayoutInflater.from(ctx);
			v = inflater.inflate(R.layout.symptom_text, null);
		}
		TextView tv = (TextView) v.findViewById(R.id.symptomtext);
		ImageView iv = (ImageView) v.findViewById(R.id.search_icon);
		
		if(!changeIcon) {
			final int selPosition = position;
			iv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					Message msg = new Message();
					msg.what = 1;
					msg.arg1 = selPosition;
					uiHandler.dispatchMessage(msg);
					
				}
			});
		}else {
			iv.setImageResource(R.drawable.left_arrow_small);
		}
		String decodedSym = "";
		
		try {
			decodedSym = URLDecoder.decode(symptoms.get(position), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		tv.setText("   " + decodedSym);

		return v;
	}

}
